package com.baox.agridata.synchronous.util;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateUtil {

    public static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");

    public static SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd");


    public static SimpleDateFormat dateFileNameFormat = new SimpleDateFormat("yyyyMMdd");

    public static SimpleDateFormat tagStrFormat = new SimpleDateFormat("yyMMddHHmm");


    public static String dateToString(Date date) {
        String s = dateFormat.format(date);
        return s;
    }


    public static String dateToMinute(Date date) {
        String s = dateFormat1.format(date);
        return s;
    }

    public static String dateCoverYearMonthDay() {
        String s = dateFormat2.format(new Date());
        return s;
    }


    public static String dateConvertTag(Date date) {
        String s = tagStrFormat.format(date);
        return s;
    }

    public static String longToString(long date) {

        Date date1 = new Date(date);

        String strDate = dateFormat.format(date1);

        return strDate;
    }


    public static String tenDigitTimestamp() {
        // 获取当前时间戳
        long currentTimeMillis = System.currentTimeMillis();

        // 将时间戳转换为字符串
        String timestamp = String.valueOf(currentTimeMillis);

        // 截取字符串的前10位
        String tenDigitTimestamp = timestamp.substring(0, 10);

        return tenDigitTimestamp;
    }


    public static String plusFiveMinutes(String timeStr) {
        // 定义时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将字符串解析为LocalDateTime对象
        LocalDateTime time = LocalDateTime.parse(timeStr, formatter);
        // 加五分钟
        LocalDateTime newTime = time.plusMinutes(5);
        // 将新时间格式化为字符串
        String newTimeStr = newTime.format(formatter);
//        // 输出结果
//        System.out.println("原时间：" + timeStr);
//        System.out.println("新时间：" + newTimeStr);
        return newTimeStr;
    }


    public static String jianFiveMinutes(String timeStr) {
        // 定义时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 将字符串解析为LocalDateTime对象
        LocalDateTime time = LocalDateTime.parse(timeStr, formatter);
        // 加五分钟
        LocalDateTime newTime = time.minusMinutes(5);
        // 将新时间格式化为字符串
        String newTimeStr = newTime.format(formatter);
//        // 输出结果
//        System.out.println("原时间：" + timeStr);
//        System.out.println("新时间：" + newTimeStr);
        return newTimeStr;
    }


    public static String jianOneMinutes() {
        Date now = new Date();
        long oneMinuteInMillis = 60 * 1000;
        Date beforeOneMinute = new Date(now.getTime() - oneMinuteInMillis);
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String formattedTime = formatter.format(beforeOneMinute);
        return formattedTime;
    }


    public static void main(String[] args) {
        System.out.println(jianOneMinutes());
    }


    /**
     * 计算量时间差（返回的是秒）
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static long diffSeconds(String startTime, String endTime) {
        try {
            Date date1 = dateFormat.parse(startTime);
            Date date2 = dateFormat.parse(endTime);

            // 计算两个时间的毫秒时间戳差
            long difference = date2.getTime() - date1.getTime();
            // 将毫秒转换为秒
            long diffSeconds = difference / 1000;
            return diffSeconds;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


}
